package com.pencilisland.pay.mgt.mp.service.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pencilisland.pay.api.bean.mp.mgt.MessageRequest;
import com.pencilisland.pay.api.bean.mp.mgt.MessageResponse;
import com.pencilisland.pay.api.bean.mp.mgt.SyncOrderREQ;
import com.pencilisland.pay.api.bean.mp.mgt.SyncOrderRES;
import com.pencilisland.pay.mgt.app.entity.GoodsEntity;
import com.pencilisland.pay.mgt.app.entity.OrderInfoEntity;
import com.pencilisland.pay.mgt.app.entity.TenantEntity;
import com.pencilisland.pay.mgt.app.repository.GoodsRepository;
import com.pencilisland.pay.mgt.app.repository.OrderInfoRepository;
import com.pencilisland.pay.mgt.app.repository.TenantRepository;
import com.pencilisland.pay.mgt.mp.service.MBService;

/**
 * 报文逻辑处理.同步订单信息
 *
 * @author breeze
 */
@Service("MB_syncOrder")
public class SyncOrderMB implements MBService {

	Logger log = LoggerFactory.getLogger(getClass());

	@Autowired
	ObjectMapper objectMapper;
	@Autowired
	GoodsRepository goodsRepo;
	@Autowired
	TenantRepository tenantRepo;
	@Autowired
	OrderInfoRepository orderInfoRepo;

	@Override
	public MessageResponse process(MessageRequest request) {
		SyncOrderREQ requestBean = getRequestBody(request);

		log.info("接收订单信息同步【{}】【{}】", requestBean.getOrderNo(), requestBean.getType());

		if ("prepay".equals(requestBean.getType())) {
			OrderInfoEntity orderInfo = new OrderInfoEntity();
			try {
				BeanUtils.copyProperties(orderInfo, requestBean);
			} catch (Exception e) {
				e.printStackTrace();
			}
			GoodsEntity goods = goodsRepo.findByCode(requestBean.getGoodsCode());
			if (goods != null) {
				orderInfo.setGoodsName(goods.getName());
				orderInfo.setTenantCode(goods.getMerchantId());

				TenantEntity tenant = tenantRepo.findByCode(goods.getMerchantId());
				if (tenant != null)
					orderInfo.setTenantName(tenant.getName());
			}

			orderInfoRepo.save(orderInfo);
		} else if ("wxpay_success".equals(requestBean.getType()) || "wxpay_fail".equals(requestBean.getType())) {
			OrderInfoEntity orderInfo = orderInfoRepo.findOne(requestBean.getOrderNo());
			if (orderInfo != null) {
				orderInfo.setTransactionId(requestBean.getTransactionId());
				orderInfo.setFinishTime(requestBean.getFinishTime());
				orderInfo.setStatus(requestBean.getStatus());
				orderInfoRepo.save(orderInfo);
			}
		}else if ("auto_close".equals(requestBean.getType())) {
			OrderInfoEntity orderInfo = orderInfoRepo.findOne(requestBean.getOrderNo());
			if (orderInfo != null) {
				orderInfo.setFinishTime(requestBean.getFinishTime());
				orderInfo.setStatus(requestBean.getStatus());
				orderInfoRepo.save(orderInfo);
			}
		}

		SyncOrderRES res = new SyncOrderRES(true, null, "OK");
		return res;
	}

	public SyncOrderREQ getRequestBody(MessageRequest request) {
		return objectMapper.convertValue(request.getBody(), SyncOrderREQ.class);
	}

}
